from google.appengine.api import images
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.api import urlfetch

from google.appengine.ext.webapp.util import run_wsgi_app

class MyHandler(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    if user:
      greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
                  (user.nickname(), users.create_logout_url("/")))
    else:
      greeting = ("<a href=\"%s\">Sign in or register</a>." %
                  users.create_login_url("/"))
	
    self.response.out.write("<html><body>%s</body></html>" % greeting)
  

class Chat (db.Model):
 creator = db.UserProperty()
 peopleinchat = db.ListProperty(str)
 chatName= db.StringProperty()
 message = db.TextProperty()

class createChat(webapp.RequestHandler):
 def post(self):
  chat = Chat() 
  if users.get_current_user():
    creator = users.get_current_user()
  peopleinchat = self.request.get("peopleUwantToInvite")
  chat.chatName = self.request.get("chatName")
  chat.put()

class deleteAChat(webapp.RequestHandler):  
  def get(self):
    chatName = self.request.get("chatName")
    query = Chat.all()
    query.filter('chatName =', chatName)
    chat = query.get()
    db.delete(chat)
	
class givebackchat(webapp.RequestHandler):
  def get(self):
    chatName = self.request.get("chatName")
    query = Chat.all()
    query.filter('chatName =', chatName)
    chat = query.get()
    self.response.out.write(chat.message)

class updatechat (webapp.RequestHandler):
  def get(self):
    message = self.request.get("message" )
    chatName = self.request.get("chatName")
    query = Chat.all()
    query.filter('chatName =', chatName)
    chat = query.get()
    chat.message = chat.message+ message 
    chat.put()
class getListofChats(webapp.RequestHandler):
  def get(self):
    query = Chat.gql('WHERE peopleinchat = :1', users.get_current_user().email)
    chats = query.fetch()
    for chat in chats:
                  self.response.out.write(chat.chatName +"/n")
      
  
application = webapp.WSGIApplication(
                                     [('/', MyHandler)
									 ('/create', createChat)
									 ('/delete', deleteAChat)
									 ('/giveChat', givebackchat)
									 ('/update', updatechat)
									 ('list', getListofChats)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
